package use.thread.sync.cooperation;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class WaitNotifyExample {

    public synchronized void before() {
        System.out.println("before");
        notify();
    }

    public synchronized void after() {
        try {
            System.out.println(Thread.currentThread().getId());
            wait();
            //Thread.sleep(2000);

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getId());
        System.out.println("after");
    }

    public static void main(String[] args) {
        WaitNotifyExample waitNotifyExample = new WaitNotifyExample();
        WaitNotifyExample waitNotifyExample1 = new WaitNotifyExample();
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            executorService.execute( () -> {
                waitNotifyExample.after();
            });
        }
        executorService.execute( () -> {
            waitNotifyExample.before();
        });
    }
}

